package com.ss.android.account.b.a;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.text.TextUtils;

import com.bytedance.common.utility.collection.WeakHandler.Callback;
import com.bytedance.common.utility.collection.WeakHandler;
import com.bytedance.common.utility.UIUtils;
import com.bytedance.common.utility.collection.WeakList;
import com.ss.android.account.Spipe;
import com.ss.android.account.model.BaseUser;
import com.ss.android.article.browser.R;
import com.ss.android.common.util.NetworkUtils;

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

public final class b implements Callback {
    public interface b$a {
        void a(BaseUser arg1);

        void a(int arg1, int arg2, BaseUser arg3);
    }

    public final Context a;
    private static b b;
    private final Handler c;
    private WeakList d;
    private Set e;

    private b(Context arg3) {

        this.a = arg3.getApplicationContext();
        this.c = new WeakHandler(Looper.getMainLooper(), ((Callback) this));
        this.d = new WeakList();
        this.e = new HashSet();
        Spipe.inst();
    }

    public static b a(Context arg2) {
        b v0_1;
        Class v1 = b.class;
        synchronized (v1) {
            if (b.b == null) {
                b.b = new b(arg2);
            }

            v0_1 = b.b;
        }
        return v0_1;
    }

    public final void a(b$a arg2) {
        if (arg2 != null) {
            this.d.add(arg2);
        }
    }

    public final boolean a(BaseUser arg2, boolean arg3, String arg4) {
        return this.a(arg2, arg3, arg4, this.a);
    }

    public final boolean a(BaseUser arg7, boolean arg8, String arg9, Context arg10) {
        return this.a(arg7, arg8, arg9, arg10, false);
    }

    public final boolean a(BaseUser arg6, boolean arg7, String arg8, Context arg9, boolean arg10) {
        int v1_1 = 0;
        int v0;
        int v3 = R.drawable.close_popup_textpage;
        boolean v1 = false;
        if (arg9 != null) {
            if (!NetworkUtils.isNetworkAvailable(arg9)) {
                android.support.design.a.showImageToast(arg9, R.string.ss_error_no_connections, v3);
            } else if (arg6 != null) {
                if (arg6.hasBlockRelation()) {
                    Context v2 = this.a;
                    if (arg6.isBlocking()) {
                        v0 = R.string.user_toast_has_blocking;
                    } else if (arg6.isBlocked()) {
                        v0 = R.string.user_toast_has_blocked;
                    } else {
                        v0 = 0;
                    }

                    android.support.design.a.showImageToast(v2, v0, v3);
                } else {
                    arg6.mIsLoading = true;
                    v0 = arg7 ? 1 : 2;
                    Handler v4 = this.c;
                    v3 = arg7 ? 100 : 101;
                    Message v3_1 = v4.obtainMessage(v3);
                    if (arg10) {
                        v1_1 = 1;
                    }

                    v3_1.arg2 = v1_1;
                    new com.ss.android.account.b.a.a(v0, v3_1, arg6, arg8).start();
                    v1 = true;
                }
            }
        }

        return v1;
    }

    public final void b(b$a arg2) {
        if (arg2 != null) {
            this.d.remove(arg2);
        } else {
            this.d.weakHashMap.size();
        }
    }

    public final void handleMsg(Message message) {
        int v0_2;
        Object v1;
        int v10 = R.drawable.close_popup_textpage;
        int v9 = 103;
        int v8 = 102;
        int v3 = R.string.user_toast_has_blocked;
        if (message != null) {
            int v4 = message.arg1;
            if (v4 == 105) {
                Spipe v0 = Spipe.inst();
                if (v0.isLogin) {
                    v0.a(true);
                }
            }

            Object v0_1 = null;
            if ((message.obj instanceof BaseUser)) {
                v0_1 = message.obj;
                this.e.remove(Long.valueOf(((BaseUser) v0_1).mUserId));
                v1 = v0_1;
            } else {
                v1 = v0_1;
            }

            switch (message.what) {
                case 100: {
                    v0_2 = R.string.user_toast_follow;
                    break;
                }
                case 101: {
                    v0_2 = R.string.user_toast_unfollow;
                    break;
                }
                case 102: {
                    v0_2 = R.string.user_toast_block;
                    break;
                }
                case 103: {
                    v0_2 = R.string.user_toast_unblock;
                    break;
                }
                default: {
                    v0_2 = 0;
                    break;
                }
            }

            switch (message.what) {
                case 100:
                case 101:
                case 102:
                case 103: {
                    if (v1 != null) {
                        ((BaseUser) v1).mIsLoading = false;
                    }

                    if (v4 != 1009) {
                        if (message.what == 100) {
                            v0_2 = ((BaseUser) v1).isBlocked() ? v3 : R.string.a2m;
                            v3 = v0_2;
                        } else {
                            if (message.what == v8) {
                                if (((BaseUser) v1).isBlocked()) {
                                } else {
                                    v3 = R.string.a2k;
                                }
                            } else if (message.what == v9) {
                                if (((BaseUser) v1).isBlocked()) {
                                } else {
                                    v3 = R.string.a2q;
                                }
                            } else if (((BaseUser) v1).isBlocked()) {
                            } else {
                                v3 = R.string.unfollow_failed;
                            }
                        }

                        if (TextUtils.isEmpty(((BaseUser) v1).mFailReason)) {
                            android.support.design.a.showImageToast(this.a, v3, v10);
                        } else {
                            android.support.design.a.showToast(this.a, ((BaseUser) v1).mFailReason, this.a.getResources().getDrawable(v10));
                        }
                    } else {
                        if (v0_2 <= 0) {
                        } else {
                            if (message.arg2 == 0) {
                                UIUtils.showImageToast(this.a, R.drawable.doneicon_popup_textpage, v0_2);
                            }
                            if (message.what != v8 && message.what != v9) {
                            } else if (v1 == null) {
                            } else {
                                ((BaseUser) v1).setIsFollowed(false);
                                ((BaseUser) v1).setIsFollowing(false);
                            }
                        }
                    }

                    Iterator v2 = this.d.iterator();
                    while (v2.hasNext()) {
                        v0_1 = v2.next();
                        if (v0_1 == null) {
                            continue;
                        }

                        ((b$a) v0_1).a(v4, message.what, ((BaseUser) v1));
                    }
                }
                case 104: {
                    Iterator v2 = this.d.iterator();
                    while (true) {
                        if (!v2.hasNext()) {
                            return;
                        }

                        v0_1 = v2.next();
                        if (v0_1 == null) {
                            continue;
                        }
                        ((b$a) v0_1).a(((BaseUser) v1));
                    }
                }
            }
            return;
        }
    }
}

